/*
 * Copyright by Deppon and the original author or authors.
 * 
 * This document only allow internal use ,Any of your behaviors using the file
 * not internal will pay legal responsibility.
 *
 * You may learn more information about Deppon from
 *
 *
 *      http://www.deppon.com
 *
 */
package com.rfy.framework.util;

import com.rfy.base.constant.GlobalConstants;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * @作者：易水清
 * @创建时间：2012-1-17
 * @功能描述：操作Cookie的常用方法
 */
public class CookieUtil {


	/**
	 * 获取所有cookie
	 * @return
	 */
	public Cookie[] readAllCookie(){
		return ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest().getCookies();
	}

	/**
	 * 读取Cookie中的值
	 * @param key
	 * @return
	 */
	public String readCookie(String key) {
		Cookie[] cookie = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest().getCookies();
		if (cookie == null) {
			return null;
		}
		for (int i = 0; i < cookie.length; i++) {
			if (cookie[i].getName().equals(key)) {
				// 解码
				String value = cookie[i].getValue();
				try {
					if (StringUtil.isNotEmpty(value)) {
						value = URLDecoder.decode(value, GlobalConstants.ENCODING_UTF8);
					}
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
					return null;
				}
				return value;
			}
		}
		return null;
	}


	/**
	 * 写入cookie
	 *
	 * @param key
	 * @param value
	 * @param maxAge
	 * @param isHttpOnly
	 */
	public void writeCookie(String key, String value, int maxAge, boolean isHttpOnly) {
		Cookie cookie;
		try {
			if (StringUtil.isNotEmpty(key)) {
				value = URLEncoder.encode(value == null ? "" : value, "utf-8");
				cookie = new Cookie(key, value);
				if (maxAge != 0) {
					cookie.setMaxAge(maxAge);
				}
				cookie.setPath("/");
				cookie.setHttpOnly(isHttpOnly);
				((ServletRequestAttributes) RequestContextHolder
						.getRequestAttributes()).getResponse()
						.addCookie(cookie);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 对writeCookie的重载，增加domain、isSecure参数,用于跨域登陆
	 *
	 * @param key
	 * @param value
	 * @param maxAge
	 * @param domain
	 * @param isSecure
	 * @param isHttpOnly
	 */
	public void writeCookie(String key, String value, int maxAge,
			String domain, boolean isSecure, boolean isHttpOnly) {
		Cookie cookie;
		try {
			if (StringUtil.isNotEmpty(key)) {
				value = URLEncoder.encode(value == null ? "" : value, "utf-8");
				cookie = new Cookie(key, value);
				if (maxAge != 0) {
					cookie.setMaxAge(maxAge);
				}
				cookie.setPath("/");
				cookie.setDomain(domain);
				cookie.setSecure(isSecure);
				cookie.setHttpOnly(isHttpOnly);
				((ServletRequestAttributes) RequestContextHolder
						.getRequestAttributes()).getResponse()
						.addCookie(cookie);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @功能描述： 清空cookies
	 * @创建人：赵本兵
	 * @创建日间：2012-3-18
	 * @param key
	 *            cookie 键值名
	 * @返回类型：void
	 * @修改日间：
	 * @修改人：
	 * @修改描述：
	 * @是否废弃：否
	 */
	public void clearCookie(String key) {
		writeCookie(key, "", 0, false);
	}

	/**
	 * 清空cookie方法重载，增加domain、isSecure参数,用于登出
	 */
	public void clearCookie(String key, String domain) {
		writeCookie(key, "", 0, false);
		writeCookie(key, "", 0, domain, false, false);
	}
}
